# Maintainer: Patrick Stewart <patstew@gmail.com>

_realname=jupyter_core
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=4.6.1
pkgrel=1
pkgdesc="Core common functionality of Jupyter projects (mingw-w64)"
arch=('any')
url="https://jupyter.org/"
license=('BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/jupyter/jupyter_core/archive/${pkgver}.tar.gz")
sha256sums=('f08266c2ff4fa7bdb9c0a9b1f22305b9a5a92f991df246e2c3ec65664627f1f4')

prepare() {
  cd "${srcdir}"
  for builddir in python{2,3}-build-${CARCH}; do
    rm -rf ${builddir}
    cp -r "${_realname}-${pkgver}" "${builddir}"
  done
  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=$pkgver
}

# Note that build() is sometimes skipped because it's done in 
# the packages setup.py install for simplicity if you can do so.
# but sometimes, you want to do a check before install which would
# also trigger the build.
build() {
  for pver in {2,3}; do  
    msg "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done  
}

all_build() {

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/${1} setup.py install \
    --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  find "${pkgdir}/" -name "*.pyc" -delete
  find "${pkgdir}/" -type d -empty -delete

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done
}

package_python3-jupyter_core() {
  local interpreter=python3
  depends=("${MINGW_PACKAGE_PREFIX}-python3")
  install=${_realname}3-${CARCH}.install
  cd "${srcdir}/python3-build-${CARCH}"

  all_build ${interpreter}

  install -Dm644 "COPYING.md" "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE"
}

package_python2-jupyter_core() {
  local interpreter=python2
  depends=("${MINGW_PACKAGE_PREFIX}-python2")
  install=${_realname}2-${CARCH}.install
  cd "${srcdir}/python2-build-${CARCH}"

  all_build ${interpreter}

  install -Dm644 "COPYING.md" "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"

  cd "${pkgdir}${MINGW_PREFIX}"/bin
  for _f in *; do
    mv ${_f} ${_f/%.*/}2${_f/${_f/%.*/}/}
  done
}

package_mingw-w64-i686-python2-jupyter_core() {
  package_python2-jupyter_core
}

package_mingw-w64-i686-python3-jupyter_core() {
  package_python3-jupyter_core
}

package_mingw-w64-x86_64-python2-jupyter_core() {
  package_python2-jupyter_core
}

package_mingw-w64-x86_64-python3-jupyter_core() {
  package_python3-jupyter_core
}
